from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime
from app.models.graph import NodeType, RelationType, PropertyType

class PropertyBase(BaseModel):
    key: str
    value: str
    type: PropertyType

class PropertyCreate(PropertyBase):
    pass

class Property(PropertyBase):
    id: str
    node_id: str
    create_time: datetime
    update_time: datetime

    class Config:
        from_attributes = True

class NodeBase(BaseModel):
    type: NodeType
    name: str
    description: Optional[str] = None
    extra_data: Optional[Dict[str, Any]] = None

class NodeCreate(NodeBase):
    properties: Optional[List[PropertyCreate]] = None

class NodeUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    extra_data: Optional[Dict[str, Any]] = None
    properties: Optional[List[PropertyCreate]] = None

class Node(NodeBase):
    id: str
    create_time: datetime
    update_time: datetime
    properties: List[Property] = []

    class Config:
        from_attributes = True

class RelationBase(BaseModel):
    type: RelationType
    source_id: str
    target_id: str
    weight: Optional[int] = 1
    extra_data: Optional[Dict[str, Any]] = None

class RelationCreate(RelationBase):
    pass

class RelationUpdate(BaseModel):
    type: Optional[RelationType] = None
    weight: Optional[int] = None
    extra_data: Optional[Dict[str, Any]] = None

class Relation(RelationBase):
    id: str
    create_time: datetime
    update_time: datetime

    class Config:
        from_attributes = True

class GraphResponse(BaseModel):
    """知识图谱查询响应"""
    nodes: List[Node]
    relations: List[Relation]

class SearchResult(BaseModel):
    """搜索结果"""
    total: int
    items: List[Node]

class PathResult(BaseModel):
    """路径查询结果"""
    paths: List[List[Dict[str, Any]]]
    total: int 